<template>
  <div class="container">
    <div class="header">
      <h1 class="logo">智合云图库AI助手</h1>
      <div class="engine-tag">Powered by DeepSeek</div>
      <div class="divider"></div>
    </div>

    <div class="chat-container">
      <div class="history-container">
        <div
          v-for="(message, index) in history"
          :key="index"
          class="message-bubble"
          :class="message.role"
        >
          <div class="message-content">
            <div class="message-header">
              <span class="role-tag">{{ message.role === 'user' ? '您' : 'AI助手' }}</span>
            </div>
            <pre class="message-text">{{ message.content }}</pre>
            <div v-if="message.loading" class="thinking-container">
              <div class="dot-flashing"></div>
              <span class="thinking-text">深度思考中...</span>
            </div>
          </div>
        </div>
      </div>

      <div class="input-container">
        <input
          v-model="query"
          type="text"
          class="query-input"
          placeholder="输入您的问题..."
          @keyup.enter="sendQuery"
          :disabled="isSending"
        />
        <button @click="sendQuery" class="query-btn" :disabled="isSending">
          <span v-if="!isSending">发送</span>
          <span v-else>发送中...</span>
        </button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, nextTick } from 'vue'

interface ThoughtProcess {
  type: 'analysis' | 'tool' | 'reasoning' | 'conclusion'
  content: string
  timestamp: number
}

interface Message {
  role: 'user' | 'assistant'
  content: string
  thoughts?: ThoughtProcess[] // 新增思考过程字段
  loading?: boolean
}

interface Message {
  role: 'user' | 'assistant'
  content: string
  loading?: boolean
}

const query = ref('')
const history = ref<Message[]>([])
const isSending = ref(false)

const sendQuery = async () => {
  const question = query.value.trim()
  if (!question || isSending.value) return

  // 添加用户消息
  history.value.push({
    role: 'user',
    content: question,
  })

  // 添加初始AI消息并记录位置
  const responseIndex = history.value.length
  history.value.push({
    role: 'assistant',
    content: '',
    loading: true,
  })

  query.value = ''
  isSending.value = true

  try {
    const encodedQuery = encodeURIComponent(question)
    const responseStream = await fetch(
      `http://localhost:10003/client/ai/stream?query=${encodedQuery}`,
    )

    if (!responseStream.ok) {
      throw new Error(`请求失败: ${responseStream.status}`)
    }

    if (!responseStream.body) throw new Error('无响应内容')

    const reader = responseStream.body.getReader()
    const decoder = new TextDecoder('utf-8')

    while (true) {
      const { done, value } = await reader.read()
      if (done) break

      const chunk = decoder.decode(value)
      if (history.value[responseIndex]) {
        history.value[responseIndex].content += chunk
        await nextTick()
        scrollToBottom()
      }
    }
  } catch (error) {
    if (history.value[responseIndex]) {
      history.value[responseIndex].content = `\n请求失败: ${error.message}`
    }
  } finally {
    if (history.value[responseIndex]) {
      history.value[responseIndex].loading = false
    }
    isSending.value = false
    scrollToBottom()
  }
}

const scrollToBottom = () => {
  const container = document.querySelector('.history-container')
  container?.scrollTo(0, container.scrollHeight)
}
</script>

<style scoped>
.container {
  max-width: 1440px;
  margin: 0 auto;
  padding: 2rem;
  min-height: 100vh;
  background: #f8fafc;
  font-family:
    'Inter',
    system-ui,
    -apple-system,
    sans-serif;
}

.header {
  text-align: center;
  margin-bottom: 2.5rem;
}

.logo {
  font-size: 2.75rem;
  font-weight: 700;
  color: #1a365d;
  margin-bottom: 0.5rem;
  letter-spacing: -0.03em;
}

.engine-tag {
  font-size: 1.1rem;
  color: #4caf50;
  font-weight: 500;
  margin-bottom: 1rem;
}

.divider {
  width: 80px;
  height: 4px;
  background: linear-gradient(90deg, #4caf50 0%, #43a047 100%);
  margin: 1.5rem auto;
  border-radius: 2px;
}

.chat-container {
  max-width: 1000px;
  margin: 0 auto;
  border-radius: 20px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.08);
  background: white;
  border: 1px solid #e2e8f0;
}

.history-container {
  height: 70vh;
  padding: 2rem;
  overflow-y: auto;
  background: linear-gradient(180deg, #f8fafc 0%, #ffffff 100%);
}

.message-bubble {
  margin-bottom: 2rem;
  transition: opacity 0.3s ease;
}

.message-content {
  max-width: 80%;
  padding: 1.5rem;
  border-radius: 16px;
  position: relative;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.message-bubble.user {
  display: flex;
  justify-content: flex-end;
}

.message-bubble.user .message-content {
  background: #4caf50;
  color: white;
  border-radius: 16px 16px 0 16px;
}

.message-bubble.assistant .message-content {
  background: white;
  border: 1px solid #e2e8f0;
  border-radius: 16px 16px 16px 0;
}

.role-tag {
  font-size: 0.85rem;
  font-weight: 600;
  color: #718096;
  margin-bottom: 0.75rem;
  display: block;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.user .role-tag {
  color: rgba(255, 255, 255, 0.8);
}

.message-text {
  white-space: pre-wrap;
  margin: 0;
  font-size: 1.05rem;
  line-height: 1.7;
  color: #2d3748;
  font-family: 'Inter', sans-serif;
}

.user .message-text {
  color: white;
}

.thinking-container {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-top: 1.5rem;
}

.thinking-text {
  color: #718096;
  font-size: 0.95rem;
  font-weight: 500;
}

.input-container {
  display: flex;
  gap: 1.5rem;
  padding: 2rem;
  background: white;
  border-top: 1px solid #e2e8f0;
  border-radius: 0 0 20px 20px;
}

.query-input {
  flex: 1;
  padding: 1.25rem 2rem;
  border: 2px solid #e2e8f0;
  border-radius: 12px;
  font-size: 1.1rem;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  background: white;
}

.query-input:focus {
  border-color: #4caf50;
  box-shadow: 0 0 0 4px rgba(76, 175, 80, 0.15);
  outline: none;
}

.query-btn {
  padding: 1.25rem 2.5rem;
  border: none;
  border-radius: 12px;
  background: linear-gradient(135deg, #4caf50 0%, #43a047 100%);
  color: white;
  font-weight: 600;
  font-size: 1.1rem;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 4px 6px rgba(76, 175, 80, 0.15);
}

.query-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 6px 12px rgba(76, 175, 80, 0.2);
}

.query-btn:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

/* 打字动画 */
.dot-flashing {
  position: relative;
  width: 10px;
  height: 10px;
  border-radius: 5px;
  background-color: #4caf50;
  color: #4caf50;
  animation: dotFlashing 1s infinite linear alternate;
  animation-delay: 0.5s;
}

.dot-flashing::before,
.dot-flashing::after {
  content: '';
  display: inline-block;
  position: absolute;
  top: 0;
}

.dot-flashing::before {
  left: -15px;
  width: 10px;
  height: 10px;
  border-radius: 5px;
  background-color: #4caf50;
  color: #4caf50;
  animation: dotFlashing 1s infinite alternate;
  animation-delay: 0s;
}

.dot-flashing::after {
  left: 15px;
  width: 10px;
  height: 10px;
  border-radius: 5px;
  background-color: #4caf50;
  color: #4caf50;
  animation: dotFlashing 1s infinite alternate;
  animation-delay: 1s;
}

@keyframes dotFlashing {
  0% {
    background-color: #4caf50;
  }
  50%,
  100% {
    background-color: rgba(76, 175, 80, 0.2);
  }
}

@media (max-width: 768px) {
  .container {
    padding: 1.5rem;
  }

  .logo {
    font-size: 2rem;
  }

  .history-container {
    height: 65vh;
    padding: 1.5rem;
  }

  .input-container {
    flex-direction: column;
    padding: 1.5rem;
  }

  .query-input {
    padding: 1rem;
  }

  .query-btn {
    width: 100%;
    padding: 1.2rem;
  }
}
</style>
